<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Form\OrderCancelActions;
use App\Admin\Actions\Form\OrderFinishActions;
use App\Admin\Actions\Grid\RowActions\BoundUserActions;
use App\Admin\Actions\Grid\RowActions\PayActions;
use App\Admin\Actions\Grid\Tools\CreateOrder;
use App\Admin\Actions\Grid\RowActions\PickOrder;
use App\Admin\Actions\Grid\RowActions\SendDataMessage;
use App\Admin\Actions\Grid\RowActions\SendDetailMessage;
use App\Admin\Actions\Grid\RowActions\SendMessage;
use App\Admin\Renderable\MessageRecordTable;
use App\Admin\Renderable\MessageTemplateTable;
use App\Admin\Repositories\Order;
use App\Models\AdminUser;
use App\Models\Category;
use App\Models\MessageRecord;
use App\Models\MessageTemplate;
use App\Models\OrderServiceProfile;
use App\Models\OrderServiceSubmitDetail;
use App\Models\User;
use App\Support\Data;
use App\Traits\ControllerHasTab;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Card;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Request;



class OrderController extends AdminController
{
    use ControllerHasTab;

    public function tab(): Row
    {
        $row = new Row();
        $tab = new Tab();
        $tab->add(Data::icon('record') . '订单管理', $this->renderGrid(), true);
        // $tab->addLink(Data::icon('track') . '我的订单', admin_route('order.my.index'));
        // $tab->add(Data::icon('track') . '已处理事项', $this->renderGrid(), true, 2);
        $row->column(12, $tab);
        return $row;
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Order(['orderService', 'serviceEmail', 'serviceSystem', 'report', 'syntax', 'details', 'profiles', 'polish', 'user']), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->withBorder();
            $grid->disableCreateButton();
            $grid->disableViewButton();
            $grid->scrollbarX();
            $grid->column('id')->sortable();
            $grid->column('order_name');
            $grid->column('order_sn');
            $grid->column('created_at');
            $grid->column('order_status')->display(function ($status) {
                $state = Data::getOrderStatus();
                return $state[$status];
            });

            // $grid->column('order_customer')
            //     ->if(function ($column) {
            //         return $column->getValue();
            //     })
            //     ->then(function (Grid\Column $column) {
            //         $column->display(function ($id) {
            //             return AdminUser::find($id)->username ?? '';
            //         })->label();
            //     })
            //     ->else(function (Grid\Column $column) {
            //         $column->display('领取订单')->action(PickOrder::class);
            //     });

            $grid->column('is_carefree', '全程无忧客户')->select([0 => '否', 1 => '是'], true);

            $grid->column('service')->display(function ($ids) {
                if (empty($ids)) {
                    return '未选择服务';
                }
                $arr = Data::getOrderService();
                $ids = explode(',', $ids);
                $service = [];
                foreach ($ids as $key => $val) {
                    array_push($service, $arr[$val]);
                }
                return implode(',', $service);
            })->label();
            $grid->column('order_amount');
            // $grid->column('user_id');
            $grid->column('pay_status')->display(function ($payStatus) {
                $arr = Data::getPayStatus();
                return $arr[$payStatus];
            });
            $grid->column('pay_amount');
            $grid->column('pay_type')->display(function ($payType) {
                $arr = Data::getPayType();
                // dd($payType);
                return isset($arr[$payType]) ? $arr[$payType] : '未知';
            });
            $grid->column('orderService.title');
            $grid->column('orderService.is_history', '拒稿历史')->using([0 => '无', 1 => '有'])
                ->dot(
                    [
                        0 => 'success',
                        1 => 'danger',
                    ]
                );
            $grid->column('orderService.history', '被拒期刊')->display(function ($history) {
                // $json2arr = json_decode($history, true);
                $json2arr = $history;
                if (!empty($json2arr)) {
                    // var_dump($json2arr);
                    $history = [];
                    foreach ($json2arr as $key => $val) {
                        array_push($history, $val['history_name']);
                    }
                    return $history;
                }
            })->implode(',');

            $grid->column('language')->display(function ($lang) {
                $arr = ['zh' => '中文', 'en' => '英文'];
                return isset($arr[$lang]) ? $arr[$lang] : '未选择';
            });
            // $grid->column('manuscript');
            // $grid->column('reference_file');
            $grid->column('additional');
            $grid->column('purpose')->display(function ($id) {
                $arr = Data::getOrderPurpose();
                return isset($arr[$id]) ? $arr[$id] : '';
            });
            $grid->column('subject_area')->display(function ($id) {
                return Category::find($id)->title ?? '';
            });
            $grid->column('discipline')->display(function ($id) {
                return Category::find($id)->title ?? '';
            });

            // $grid->column('updated_at')->sortable();
            $grid->column('user.realname', '用户名')->display(function ($name) {
                return $name ?? '未绑定';
            });
            $grid->column('user.phone', '手机号')->display(function ($phone) {
                return $phone ?? '未绑定';
            });

            $grid->column('report.review_report_rate', '查重报告')->help('客户自带是黄色填充') //;

                //     // ->display(2)->setAttributes(['style' => 'background:yellow']);
                ->if(function ($column) {

                    if ($this->orderService->is_review == 0) {
                        return true;
                    } else {
                        return false;
                    }
                })->then(function (Grid\Column $column) {
                    $column->display(function ($val) {
                        return "<span  style='background:yellow;display:block;width=20px;height=20px;'>$val %</span>";
                    });
                })->else(function (Grid\Column $column) {
                    $column->display($column->getValue() . ' %');
                });

            $grid->column('syntax.syntax_report_rate', '语法报告')->help('客户自带是黄色填充') //;

                // ->display(2)->setAttributes(['style' => 'background:yellow']);
                ->if(function ($column) {
                    if ($this->orderService->is_syntax == 0) {
                        return true;
                    } else {
                        return false;
                    }
                })->then(function (Grid\Column $column) {
                    $column->display(function ($val) {
                        return "<span  style='background:yellow;display:block;width=20px;height=20px;'>$val %</span>";
                    });
                })->else(function (Grid\Column $column) {
                    $column->display($column->getValue() . ' %');
                });

            $grid->column('orderService.bibliometric_report', '文献计量报告')->display(function ($report_time) {
                // var_dump($report_time);
                // $data = json_decode($report_time, 'true');
                return $report_time['bibliometric_report_time'] ?? '-';
            });

            $grid->column('orderService.journal_report', '期刊推荐报告')->display(function ($report_time) {
                // $data = json_decode($report_time, 'true');0
                return $report_time['journal_report_time'] ?? '-';
            });

            $grid->column('orderService.cover_page', '封面页')->display(function ($report_time) {
                // $data = json_decode($report_time, 'true');
                return $report_time['cover_page_time'] ?? '-';
            });

            $grid->column('orderService.contents_page', '目录页')->display(function ($report_time) {
                // $data = json_decode($report_time, 'true');
                return $report_time['contents_page_time'] ?? '-';
            });

            $grid->column('orderService.full_report', '完整报告')->display(function ($report_time) {
                // $data = json_decode($report_time, 'true');
                return $report_time['full_report_time'] ?? '-';
            });

            $grid->column('polish.polish_time', '润色/降重');
            $grid->column('orderService.is_target', '目标期刊')->using([0 => '无', 1 => '有'])
                ->dot(
                    [
                        0 => 'success',
                        1 => 'danger',
                    ]
                );
            $grid->column('orderService.target_journal', '期刊名单')->display(function ($target_journal) {
                // $json2arr = json_decode($target_journal, true);
                $json2arr = $target_journal;
                if (!empty($json2arr)) {
                    $target = [];
                    foreach ($json2arr as $key => $val) {
                        array_push($target, $val['target_journal_name']);
                    }
                    return $target;
                }
            })->implode(',');

            $grid->column('serviceSystem', '投稿系统注册信息')->view('fileds_content');

            $grid->column('details', '投稿进度/投稿沟通tracking')->view('details_content');
            $grid->column('detailsRecord', '投稿进度发送记录')->display('投稿进度发送记录')->modal(function ($modal) {
                $modal->title('投稿进度发送记录');

                // 允许在闭包内返回异步加载类的实例
                return MessageRecordTable::make()->payload(['message_type' => 1]);
            });

            $grid->column('dataRecord', '补充资料发送记录')->display('补充资料发送记录')->modal(function ($modal) {
                $modal->title('补充资料发送记录');

                // 允许在闭包内返回异步加载类的实例
                return MessageRecordTable::make()->payload(['message_type' => 2]);
            });


            // 传入闭包
            $grid->tools(function (Grid\Tools $tools) {
                $tools->append(new CreateOrder());
            });

            if (Admin::user()->isAdministrator()) {

                $grid->showEditButton();
                $grid->showDeleteButton();
                $grid->actions(function (Grid\Displayers\Actions $actions) {

                    // 支付按钮
                    if ($actions->row->pay_status == 0) {
                        $actions->append(new PayActions());
                    }
                    // 绑定客户按钮
                    if ($actions->row->user_id == null) {
                        $actions->append(new BoundUserActions());
                    }

                    // 分配

                    $actions->append(new SendDetailMessage());
                    $actions->append(new SendDataMessage());

                    // if ($actions->row->is_carefree == 1 || in_array(9, explode(',', $actions->row->service))) {
                    //     $actions->append(new IsCarefree());
                    // }
                });
            } else {
                $grid->disableActions();
            }


            $grid->filter(function (Grid\Filter $filter) {

                $filter->like('order_name');
                $filter->equal('order_sn');
                $filter->equal('pay_type')->select(Data::getPayType());
                $filter->equal('order_status')->select(Data::getOrderStatus());
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Order(), function (Show $show) {
            $show->field('id');
            $show->field('order_sn');
            $show->field('order_name');
            $show->field('order_status');
            $show->field('order_customer');
            $show->field('order_amount');
            $show->field('username');
            $show->field('phone');
            $show->field('user_id');
            $show->field('pay_status');
            $show->field('pay_amount');
            $show->field('pay_type');
            $show->field('pay_ticket');
            $show->field('service');
            $show->field('language');
            $show->field('manuscript');
            $show->field('reference_file');
            $show->field('additional');
            $show->field('purpose');
            $show->field('subject_area');
            $show->field('discipline');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Order(['orderService', 'serviceEmail', 'serviceSystem', 'report', 'syntax', 'details', 'profiles', 'polish']), function (Form $form) {

            $form->disableDeleteButton();
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->disableViewButton();

            $form->tab('订单信息', function (Form $form) {
                // tab 可以和 column 布局结合

                $form->display('id');
                $form->display('pay_status')->with(function ($value) {
                    $arr = Data::getPayStatus();
                    if ($value == 0) {
                        return $arr[$value];
                    } else {
                        return $arr[$value] . '
                        金额：' . $this->pay_amount  . '    支付凭证：' . '<img src="' . $this->pay_ticket . '" alt="支付凭证" width="150" height="120" />';
                    }
                });
                $form->display('order_sn');
                $form->display('created_at', '订单生成时间');
                // $form->radio('pay_status')->when(1, function (Form $form) {
                //     $form->text('pay_amount');
                //     $form->image('pay_ticket');
                //     $form->select('pay_type')->options(Data::getPayType());
                //     $form->datetime('pay_datetime', '支付时间');
                // })->options([0 => '待支付', 1 => '已支付']);

                $form->checkbox('service')->options(Data::getOrderService())->canCheckAll()->saving(function ($value) {
                    // 也可以转化为json
                    return implode(',', $value);
                });
                $form->select('language')->options(['zh' => '中文', 'en' => '英文']);
                $form->table('manuscript', '上传稿件', function ($table) {
                    $table->file('manuscript_file', '稿件')->autoUpload()->move('medical/image/' . date('Y/m') . '/' . uniqid())->saveFullUrl();
                    $table->text('manuscript_number', '字数');
                })->saving(function ($v) {
                    return json_encode($v);
                });

                // $form->table('reference_file', '参考文件', function ($table) {
                //     $table->file('file', '上传参考文件')->autoUpload()->move('medical/image/' . date('Y/m') . '/' . uniqid())->saveFullUrl();
                // })->saving(function ($v) {
                //     return json_encode($v);
                // });
                $form->multipleFile('reference_file', '参考文件')->saveFullUrl()->autoUpload()->saving(function ($paths) {
                    // 也可以转化为json
                    return implode(',', $paths);
                });
                $form->textarea('additional');
                $form->select('purpose', '稿件用途')->options(Data::getOrderPurpose());

                $form->select('subject_area', '学科领域')->options(
                    Category::where('parent_id', 0)->pluck('title', 'id')
                )
                    ->load('discipline', '/getOrderDiscipline');
                $form->select('discipline', '细分学科')->options(function ($id) {
                    return  Category::where('id', $id)->pluck('title', 'id');
                });
                // 完成
                $form->tools(function (Form\Tools $tools) use ($form) {

                    if ($form->model()->order_status == 0) {
                        $tools->append(new OrderCancelActions());
                    } elseif ($form->model()->order_status == 1) {
                        $tools->append(new OrderFinishActions());
                    }
                });
                // 取消

            })->tab('服务信息', function (Form $form) {

                $form->text('order_name', '订单名称')->help('规则：企业-领域-客户姓名-工作内容');

                if ($form->model()->is_carefree == 1) {
                    $form->select('is_carefree', '全程无忧客户')->when(1, function (Form $form) {
                        $form->hasMany('serviceEmail', '投稿邮箱信息', function ($form) {
                            $form->text('email', '投稿邮箱');
                            $form->text('password', '邮箱密码');
                        });
                        $form->hasMany('serviceSystem', '投稿系统注册信息', function ($form) {
                            $form->text('system_name', '系统名称');
                            $form->text('website', '网址');
                            $form->text('email', '邮箱');
                            $form->text('password', '密码');

                            $form->table('schedule', '投稿目标期刊', function ($table) {
                                $table->text('submit_reg_info_periodical', '期刊名');
                                $table->text('submit_reg_info_influence_factor', '影响因子');
                                $table->select('submit_reg_info_status', '投稿状态')->options([1 => '投稿中', 2 => '已接收', 3 => '被拒绝']);
                                $table->file('submit_reg_files', '文件')->autoUpload()->url('users/files')->saveFullUrl();
                            })->saving(function ($v) {
                                return json_encode($v);
                            });
                        });

                        $form->hasMany('details', '投稿详情', function (Form\NestedForm $form) {
                            $form->switch('status', '发送短信')->help('设置为"不发短信”后，系统待办事项将不再进行该条沟通记录的发送提醒');
                            $form->datetime('datetime', '时间');
                            // $form->selectTable('template_id', '选择模板')
                            //     ->title('选择模板')
                            //     ->dialogWidth('80%') // 弹窗宽度，默认 800px
                            //     ->from(MessageTemplateTable::make(['id' => $form->getKey()])) // 设置渲染类实例，并传递自定义参数
                            //     ->model(MessageTemplate::class, 'id', 'template_content'); // 设置编辑数据显示
                            $form->textarea('contents', '内容');

                            // $form->button('发送短信')->attribute(['id' => 'click-button', 'value' => '222']);
                        });
                    })->options([0 => '否', 1 => '是']);
                } elseif (in_array(9, explode(',', $form->model()->service))) {

                    $form->hasMany('serviceEmail', '投稿邮箱信息', function ($form) {
                        $form->text('email', '投稿邮箱');
                        $form->text('password', '邮箱密码');
                    });
                    $form->hasMany('serviceSystem', '投稿系统注册信息', function ($form) {
                        $form->text('system_name', '系统名称');
                        $form->text('website', '网址');
                        $form->text('email', '邮箱');
                        $form->text('password', '密码');

                        $form->table('schedule', '投稿目标期刊', function ($table) {
                            $table->text('submit_reg_info_periodical', '期刊名');
                            $table->text('submit_reg_info_influence_factor', '影响因子');
                            $table->select('submit_reg_info_status', '投稿状态')->options([1 => '投稿中', 2 => '已接收', 3 => '被拒绝']);
                            $table->file('submit_reg_files', '文件')->autoUpload()->url('users/files')->saveFullUrl();
                        })->saving(function ($v) {
                            return json_encode($v);
                        });
                    });

                    $form->hasMany('details', '投稿详情', function (Form\NestedForm $form) {
                        $form->switch('status', '发送短信')->help('设置为"不发短信”后，系统待办事项将不再进行该条沟通记录的发送提醒');
                        $form->datetime('datetime', '时间');
                        // $form->selectTable('template_id', '选择模板')
                        //     ->title('选择模板')
                        //     ->dialogWidth('80%') // 弹窗宽度，默认 800px
                        //     ->from(MessageTemplateTable::make(['id' => $form->getKey()])) // 设置渲染类实例，并传递自定义参数
                        //     ->model(MessageTemplate::class, 'id', 'template_content'); // 设置编辑数据显示
                        $form->textarea('contents', '内容');

                        // $form->button('发送短信')->attribute(['id' => 'click-button', 'value' => '222']);
                    });
                }


                $form->text('order_amount', '订单金额')->disable();
                $form->text('orderService.title', '论文标题');

                $form->select('orderService.is_history', '拒稿历史')->options([0 => '无', 1 => '有'])->when(1, function (Form $form) {
                    $form->table('orderService.history', '', function ($table) {
                        $table->text('history_name', '拒稿期刊名');
                        $table->text('history_influence_factor', '影响因子');
                    });
                });

                $form->radio('orderService.is_review', '查重报告')->options([0 => '自带', 1 => '填写'])->when(0, function (Form $form) {
                    $form->datetime('report.review_report_time', '完成时间')->disable();
                })->when(1, function (Form $form) {
                    $form->datetime('report.review_report_time', '完成时间');
                });
                $form->rate('report.review_report_rate', '查重率');
                $form->file('report.review_report_url', '文件')->autoUpload()->move('medical/image/' . date('Y/m') . '/' . uniqid())->saveFullUrl();


                $form->radio('orderService.is_syntax', '语法报告')->options([0 => '自带', 1 => '填写'])->when(0, function (Form $form) {
                    $form->datetime('syntax.syntax_report_time', '完成时间')->disable();
                })->when(1, function (Form $form) {
                    $form->datetime('syntax.syntax_report_time', '完成时间');
                });
                $form->rate('syntax.syntax_report_rate', '查重率');
                $form->file('syntax.syntax_report_url', '文件')->autoUpload()->move('medical/image/' . date('Y/m') . '/' . uniqid())->saveFullUrl();


                $form->embeds('orderService.bibliometric_report', '文献计量报告', function ($form) {
                    $form->datetime('bibliometric_report_time', '完成时间');
                    $form->file('bibliometric_report_url', '文件')->autoUpload()->url('users/files')->saveFullUrl();
                });

                $form->table('orderService.journal_report', '期刊推荐报告', function ($table) {
                    $table->datetime('journal_report_time', '完成时间');
                    $table->file('journal_report_url', '文件')->autoUpload()->url('users/files')->saveFullUrl();
                });


                $form->embeds('orderService.cover_page', '封面页', function ($form) {
                    $form->datetime('cover_page_time', '完成时间');
                    $form->file('cover_page_url', '文件')->autoUpload()->url('users/files')->saveFullUrl();
                });


                $form->embeds('orderService.contents_page', '目录页', function ($form) {
                    $form->datetime('contents_page_time', '完成时间');
                    $form->file('contents_page_url', '文件')->autoUpload()->url('users/files')->saveFullUrl();
                });

                $form->embeds('orderService.full_report', '完整报告', function ($form) {
                    $form->datetime('full_report_time', '完成时间');
                    $form->file('full_report_url', '文件')->autoUpload()->url('users/files')->saveFullUrl();
                });

                $form->radio('orderService.is_polish', '润色/降重')->options([0 => '自带', 1 => '填写'])->when(0, function (Form $form) {
                    $form->datetime('polish.polish_time', '完成时间')->disable();
                })->when(1, function (Form $form) {
                    $form->datetime('polish.polish_time', '完成时间');
                });
                $form->file('polish.polish_url', '文件')->autoUpload()->move('medical/image/' . date('Y/m') . '/' . uniqid())->saveFullUrl();

                $form->select('orderService.is_target', '目标期刊')->options([0 => '无', 1 => '有'])->when(1, function (Form $form) {
                    $form->table('orderService.target_journal', '', function ($table) {
                        $table->text('target_journal_name', '期刊名');
                        $table->text('target_journal_influence_factor', '影响因子');
                    });
                });

                $form->file('orderService.author_info', '作者信息表')->autoUpload()->move('medical/image/' . date('Y/m') . '/' . uniqid())->saveFullUrl();



                $form->hasMany('profiles', '补充资料', function ($table) {
                    $table->text('name', '说明')->required();
                    $table->text('files_name', '文件名称')->required();
                    $table->file('files', '文件')->autoUpload()->move('medical/image/' . date('Y/m') . '/' . uniqid())->saveFullUrl();
                    $table->textarea('contents', '沟通内容');
                    $table->text('customer_data_name', '客户资料名称');
                    $table->file('customer_data', '客户资料')->autoUpload()->move('medical/image/' . date('Y/m') . '/' . uniqid())->saveFullUrl();
                    // $table->button('发送短信')->addElementClass(['sendMessage']);
                });



                // Admin::script(
                //     <<<JS
                // $('.sendMessage').on('click',function(){
                //     console.log(123);
                // });
                // document.getElementsByClassName('sendMessage').addEventListener('click',t);
                // var elements = document.getElementsByClassName("sendMessage");
                // console.log(elements)
                // for(var i = 0; i < elements.length; i++)
                // {
                //     console.log(elements[i].click());
                // }
                // function t(){
                //     var title = $('input[name="orderService[title]"]').val();
                //     layer.confirm('确认发送？',{
                //         title:'发送短信',
                //         btn:['确认','取消']
                //     },function(){
                //         console.log('确认');
                //         layer.close();
                //     },function(){
                //         console.log('取消');
                //         layer.close();
                //     });
                //       $.ajax({
                //                 url: "/getOrderDiscipline",
                //                 data: 'q=1',
                //                 type: 'get',
                //                 contentType: false,
                //                 processData: false,
                //                 success: function (s) {
                //                     console.log(s);
                //                     // if () {

                //                     // }
                //                 },
                //                 error: function(e) {
                //                     console.error(e.responseJSON.message);

                //                 }
                //             });
                // }
                // JS
                // );
            })->tab('通知记录', function (Form $form) {
                $data = MessageRecord::where('order_id', $form->getKey())
                    ->get(['user_id', 'message_id', 'message_type', 'admin_id', 'created_at'])
                    ->toArray();

                foreach ($data as $key => &$val) {
                    $val['user_id'] = User::find($val['user_id'])->username ?? '';
                    if ($val['message_type'] == 1) {
                        $val['message_id'] = OrderServiceSubmitDetail::find($val['message_id'])->contents ?? '';
                        $val['message_type'] = '投稿详情（投稿进度）';
                    } elseif ($val['message_type'] == 2) {
                        $val['message_id'] = OrderServiceProfile::find($val['message_id'])->contents ?? '';
                        $val['message_type'] = '补充资料';
                    }
                    $val['admin_id'] = AdminUser::find($val['admin_id'])->username ?? '';
                }

                $form->html(new Card('通知记录', view('services_dashboard')
                    ->with('data', $data)));
            });


            $form->hidden('is_carefree')->saving(function ($v) {
                return $v;
            });

            $form->saving(function (Form $form) {
                // dd($form->input());
                $is_review = $form->input('orderService.is_review');
                $is_syntax = $form->input('orderService.is_syntax');
                $is_polish = $form->input('orderService.is_polish');
                if ($is_review == 0) {
                    $form->input('report.review_report_time', '');
                }
                if ($is_syntax == 0) {
                    $form->input('syntax.syntax_report_time', '');
                }
                if ($is_polish == 0) {
                    $form->input('polish.polish_time', '');
                }
            });
        });
    }

    public function getOrderDiscipline(Request $request)
    {
        $projectId = $request->get('q');
        return Category::where('parent_id', $projectId)->get(['id', DB::raw('title as text')]);
    }
}
